/*
 * UseTest.java
 */

import iRoomMonitor.*;

/**
 * Simple unit tester to verify functionality of iRoomMonitor ES API.
 * 
 * The API must be properly specified in the class path or this program will
 * not be able to function. Also ensure that the MySQL JDBC (Connector/J) is
 * contained within the API or specified in the class path.
 * 
 * @author Chris Hale
 * @version 1.1
 */

public class UseTest {


    // Assigned sensor ID, defaulting to -1 to indicate an error.
    private static int assignedID = -1;
    private static DB database;
    private static String hardwareAddress = "1234.1234.1234.1234";


    /**
     * Entry point for unit testing program.
     * @param args Command line arguments provided by the unit tester.
     */
    public static void main(String [ ] args) {

        // Connect to the Debug database.
        database = new DB();
        try {
            database.connect("cs340.pugetsound.edu/RoomNanny", "ES_API", "allmybase", true);
            System.out.println("Connected to database!");
        } catch (DBException e) {
            System.err.println("No database connection:");
            System.err.println(e.toString());
            return ;
        }

        // Register a fake sensor. Necessary before pushing any data.
        try {
            assignedID = database.registerSensor(hardwareAddress);
        } catch (DBException e) {
            System.err.println("Could not register sensor:");
            System.err.println(e.toString());
            return ;
        }

        // Push test data.
        try {
            database.pushData(assignedID, 75.0f, DoorState.Opening, false);
        } catch (DBException e) {
            System.err.println("Error pushing data:");
            System.err.println(e.toString());
            return ;
        }
        
        // Push data recorded at a specific time.
        // This method is intended to store data that had been recorded when
        // the database was down.
        try {
            database.pushBackupData(assignedID, "2010-11-01 00:00:00", 90.0f, DoorState.Closing, true);
        } catch (DBException e) {
            System.err.println("Error pushing backup data (i.e. from a specific time):");
            System.err.println(e.toString());
            return ;
        }
        
        // Updata battery status.
        try {
            database.updateBatteryStatus(assignedID, 95);
        } catch (DBException e) {
            System.err.println("Error pushing data:");
            System.err.println(e.toString());
            return ;
        }
        
        // TODO: Now that we've tested for functionality, add cases to break it
    }
}
